package com.javalong.affairs.ui;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;

import com.javalong.affairs.R;
import com.javalong.affairs.utils.DisplayUtils;

public class BottomBar extends LinearLayout implements OnClickListener {
	private List<ImageView> imgTips;
	private LayoutInflater inflater;

	public BottomBar(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		init();
	}

	public BottomBar(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
		TypedArray ta = context.obtainStyledAttributes(attrs,
				R.styleable.BottomBar);
		int N = ta.getIndexCount();
		int resId = 0;
		for (int i = 0; i < N; i++) {
			int attr = ta.getIndex(i);
			switch (attr) {
			case R.styleable.BottomBar_bgColor:
				resId = ta.getResourceId(R.styleable.BottomBar_bgColor, 0);
				if (resId > 0) {
					this.setBackgroundColor(ta.getResources().getColor(resId));
				} else {
					this.setBackgroundColor(ta.getColor(
							R.styleable.BottomBar_bgColor,
							android.R.color.white));
				}
				break;
			case R.styleable.BottomBar_bgDrawable:
				resId = ta.getResourceId(R.styleable.BottomBar_bgDrawable, 0);
				if (resId > 0) {
					this.setBackgroundDrawable(ta.getResources().getDrawable(
							resId));
				}
				break;
			}
		}

	}

	public BottomBar(Context context) {
		super(context);
		init();
	}

	private void init() {
		inflater = LayoutInflater.from(getContext());
		inflater.inflate(R.layout.ui_bottom_bar, this);
		imgTips = new ArrayList<ImageView>();
	}

	public void setRes(int[] drawables, String[] tips) {
		LinearLayout.LayoutParams ll_param = new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.WRAP_CONTENT,
				LinearLayout.LayoutParams.WRAP_CONTENT, 1);
		ll_param.gravity = Gravity.CENTER;
		LinearLayout ll = (LinearLayout) findViewById(R.id.ll_bottom);
		int size = 0;
		if (drawables != null) {
			size = drawables.length;
		}
		if (tips != null) {
			size = tips.length;
		}
		for (int i = 0; i < size; i++) {
			ImageView imgTip = new ImageView(getContext());
			if (drawables != null && drawables.length > i) {
				imgTip.setImageDrawable(getResources()
						.getDrawable(drawables[i]));
			}
			imgTip.setScaleType(ScaleType.FIT_CENTER);
			imgTip.setOnClickListener(this);
			ll.addView(imgTip, ll_param);

			imgTips.add(imgTip);
		}
	}

	public interface OnBottomClick {
		void onBottomClick(int index);
	}

	private OnBottomClick onBottonClick;

	public void setOnBottonClick(OnBottomClick onBottonClick) {
		this.onBottonClick = onBottonClick;
	}

	@Override
	public void onClick(View v) {
		int index = imgTips.indexOf(v);
		if (onBottonClick != null) {
			onBottonClick.onBottomClick(index);
		}
		for (int i = 0; i < imgTips.size(); i++) {
			if (i == index) {
				imgTips.get(i).setImageLevel(2);
			} else {
				imgTips.get(i).setImageLevel(1);
			}
		}
	}

	public void setSelection(int i) {
		imgTips.get(i).setImageLevel(2);
	}

}
